package com.qiumo.bills.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import com.qiumo.bills.domain.TBill;
import com.qiumo.bills.service.ITBillService;
import com.qiumo.bills.vo.BillPageVo;
import com.qiumo.bills.vo.BillVo;
import com.qiumo.common.annotation.Log;
import com.qiumo.common.core.controller.BaseController;
import com.qiumo.common.core.domain.AjaxResult;
import com.qiumo.common.core.page.TableDataInfo;
import com.qiumo.common.enums.BusinessType;
import com.qiumo.common.utils.SecurityUtils;
import com.qiumo.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 账单列表Controller
 *
 * @author qiumobill
 * @date 2022-06-05
 */
@RestController
@RequestMapping("/bills/bill")
public class TBillController extends BaseController {
    @Autowired
    private ITBillService tBillService;

    /**
     * 查询账单列表列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TBill tBill) {
        startPage();
        /*List<TBill> list = tBillService.selectTBillList(tBill);*/
        List<BillPageVo> list = tBillService.getPage(tBill);
        return getDataTable(list);
    }

    /**
     * 导出账单列表列表
     */
    @Log(title = "账单列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TBill tBill) {
        List<TBill> list = tBillService.selectTBillList(tBill);
        ExcelUtil<TBill> util = new ExcelUtil<TBill>(TBill.class);
        util.exportExcel(response, list, "账单列表数据");
    }

    /**
     * 获取账单列表详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tBillService.selectTBillById(id));
    }

    /**
     * 新增账单列表
     */
    @Log(title = "账单列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TBill tBill) {
        Date date = DateUtil.date();
        tBill.setCreatTime(new Date());
        tBill.setYear(DateUtil.year(date)+"");
        //获得月份，从0开始计数
        int month = DateUtil.month(date) + 1;
        tBill.setMonth(month+"");
        tBill.setDays(DateUtil.dayOfMonth(date)+"");
        tBill.setUserId(SecurityUtils.getUserId());
        return toAjax(tBillService.insertTBill(tBill));
    }

    /**
     * 修改账单列表
     */
    @Log(title = "账单列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TBill tBill) {
        return toAjax(tBillService.updateTBill(tBill));
    }

    /**
     * 删除账单列表
     */
    @Log(title = "账单列表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tBillService.deleteTBillByIds(ids));
    }

    /**
     * 查询用户账单信息
     */
    @Log(title = "查询用户账单信息")
    @GetMapping("/getBills")
    public AjaxResult getBills() {
        //获取用户ID
        Long userId = SecurityUtils.getUserId();
        Date date = DateUtil.date();
        //获得年的部分
        int year = DateUtil.year(date);
        //获得月份，从0开始计数
        int month = DateUtil.month(date) + 1;
        //获取所有消费金额
        BigDecimal allMoney = tBillService.getAllMoneyById(userId);
        //获取本月总金额
        BigDecimal MMoney = tBillService.getMMoneyById(userId,year,month);
        //获取本月消费次数
        Integer num = tBillService.getMNum(userId,year,month);
        return AjaxResult.success(MapUtil.builder()
                .put("allMoney",allMoney)
                .put("MMoney",MMoney)
                .put("num",num)
                .map());
    }

    /**
     * 查询本月消费
     */
    @Log(title = "查询本月消费")
    @GetMapping("/getList")
    public AjaxResult getList() {
        Date date = DateUtil.date();
        //获得年的部分
        int year = DateUtil.year(date);
        //获得月份，从0开始计数
        int month = DateUtil.month(date) + 1;
        List<BillVo> list = tBillService.getList(year,month);
        return AjaxResult.success(list);
    }
    /**
     *查询最近七天消费统计
     */
    @Log(title = "查询最近七天消费统计")
    @GetMapping("/getTj")
    public AjaxResult getTj() {
        /**
         * 日期列表
         */
        List<String> dayList = tBillService.getDayList();
        /**
         * 金额列表
         */
        List<BigDecimal> mList = tBillService.getMList();
        return AjaxResult.success(MapUtil.builder()
                .put("dayList",dayList)
                .put("mList",mList)
                .map());
    }

    /**
     *  查询最新五条消费统计
     */
    @Log(title = "查询最新五条消费统计")
    @GetMapping("/getBillFive")
    public AjaxResult getBillFive() {
        List<BillPageVo> list = tBillService.getBillFive();
        return AjaxResult.success(list);
    }


}
